package cn.chenfei.sshd.client.common;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.sshd.sftp.client.SftpClient;
import org.apache.sshd.sftp.common.SftpHelper;

import java.nio.file.attribute.PosixFilePermissions;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Data
@Accessors(chain = true)
public class DirInfo {

    private String fileName;
    private LocalDateTime modifyTime;
    private DirEnum type;
    private String permissions;
    private SftpClient.Attributes attributes;

    public DirInfo(String fileName, SftpClient.Attributes attributes) {
        this.attributes = attributes;
        this.fileName = fileName;
        this.modifyTime = LocalDateTime.ofInstant(attributes.getModifyTime().toInstant(), ZoneId.systemDefault());
        this.type = DirEnum.getByType(attributes.getType());
        this.permissions = this.type.prefix + PosixFilePermissions
                .toString(SftpHelper.permissionsToAttributes(attributes.getPermissions()));
    }

    /*

    public int getPermissionsInt() {
        Set<PosixFilePermission> permissionSet = PosixFilePermissions.fromString(permissions);
        return SftpHelper.attributesToPermissions(type.value == 1, type.value == 2, type.value == 3, permissionSet);
    }

    */
}